namespace f1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeTreeView();
        }

        private void InitializeTreeView()
        {
            treeView1.Nodes.Clear();
            treeView1.PathSeparator = "/";
            treeView1.HideSelection = false;
            treeView1.ExpandAll();

            // Add this line to handle NodeMouseClick event.
            treeView1.NodeMouseClick += treeView1_NodeMouseClick;

            var userFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            AddDirectoryNodes(treeView1.Nodes, userFolder);
        }


        private void AddDirectoryNodes(TreeNodeCollection nodes, string directory)
        {
            var directoryInfo = new DirectoryInfo(directory);

            foreach (var subDirectory in directoryInfo.GetDirectories())
            {
                var node = nodes.Add(subDirectory.Name);
                node.Name = subDirectory.FullName;

                try
                {
                    AddDirectoryNodes(node.Nodes, subDirectory.FullName);
                }
                catch (UnauthorizedAccessException)
                {
                    // Ignore directories we don't have access to.
                }
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            // Check if the left mouse button was clicked.
            if (e.Button == MouseButtons.Left)
            {
                listView1.Items.Clear();

                var directory = e.Node.Name;
                var directoryInfo = new DirectoryInfo(directory);

                foreach (var subDirectory in directoryInfo.GetDirectories())
                {
                    var item = listView1.Items.Add(subDirectory.Name);
                    item.Tag = subDirectory.FullName;
                }

                foreach (var file in directoryInfo.GetFiles())
                {
                    var item = listView1.Items.Add(file.Name);
                    item.Tag = file.FullName;
                }
            }
        }


        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                var item = listView1.SelectedItems[0];
                var filePath = (string)item.Tag;

                if (Path.GetExtension(filePath).ToLower() == ".txt")
                {
                    System.Diagnostics.Process.Start("notepad.exe", filePath);
                }
            }
        }

        private void toolStripButtonOpen_Click(object sender, EventArgs e)
        {
            using (var dialog = new FolderBrowserDialog())
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    treeView1.Nodes.Clear();
                    AddDirectoryNodes(treeView1.Nodes, dialog.SelectedPath);
                }
            }
        }

        private void toolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            InitializeTreeView();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }


    }
}